package com.rk.kbk.controller.util;


import com.rk.kbk.controller.config.AlipayConfig;
import com.rk.kbk.controller.util.alipay.util.SignUtils;
import com.rk.kbk.controller.util.wxpayRide.api.WXpayApi;
import com.rk.kbk.controller.util.wxpayRide.util.WXpayCoreRide;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by bilibili on 2017/5/9.
 */
public class PayUtilsRide {

    public static String sign(String content, String RSA_PRIVATE) {
        return SignUtils.sign(content, RSA_PRIVATE);
    }

    /**
     *  支付宝支付
     * @param trade_no      订单号
     * @param total_fee     金额
     * @param subject       标题
     * @param body          内容
     * @param notify_url    回调地址
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String AliPay(String trade_no, Double total_fee, String subject, String body, String notify_url) throws UnsupportedEncodingException {
        notify_url = URLEncoder.encode(notify_url, "utf-8");
        String orderInfo = "_input_charset=\"utf-8\"";
        orderInfo += "&body=" + "\"" + body + "\"";
        orderInfo += "&it_b_pay=\"30m\"";
        orderInfo += "&notify_url=" + "\"" + notify_url + "\"";
        orderInfo += "&out_trade_no=" + "\"" + trade_no + "\"";
        orderInfo += "&partner=" + "\"" + AlipayConfig.partner + "\"";
        orderInfo += "&payment_type=\"1\"";
        orderInfo += "&return_url=\"" + notify_url + "\"";
        orderInfo += "&seller_id=" + "\"" + AlipayConfig.seller_email + "\"";
        orderInfo += "&service=\"mobile.securitypay.pay\"";
        orderInfo += "&subject=" + "\"" + subject + "\"";
        String format = String.format("%.2f", total_fee);
        orderInfo += "&total_fee=" + "\"" + format+"\"";
        System.out.println(orderInfo);
        String sign = sign(orderInfo, AlipayConfig.private_key);
        sign = URLEncoder.encode(sign, "utf-8");
        return orderInfo + "&sign=\"" + sign + "\"&" + "sign_type=\"RSA\"";
    }


    /**
     *  微信支付
     * @param trade_no      订单号
     * @param total_fee     金额
     * @param body          文字内容说明
     * @param wxnotify      回调地址
     */
    public static Map WxPay(String trade_no, Double total_fee, String body, String wxnotify){
      //  JSONObject json = new JSONObject();
        Map map =new HashMap();
        System.out.println(" =============》预付款开始:");
        String format = String.format("%.2f", total_fee);
        BigDecimal total_fee_m = new BigDecimal(format).multiply(new BigDecimal("100"));
        Map<String, String> retMap = testUnifiedorder(trade_no, body, String.valueOf(total_fee_m.intValue()), wxnotify);
        System.out.println(" =============》预付款结束:");
        System.out.println(WXpayCoreRide.isRetSuccess(retMap)); // 判断统一下单（预支付）接口是否成功
        if (WXpayCoreRide.isRetSuccess(retMap)) {
            Map<String, String> appMap = WXpayApi.makePaymentMap(retMap);
            // 预支付成功，组装真正支付需要的参数，返回给app使用
            System.out.println(" =============》组装app使用参数:");
            System.out.println(appMap);
            map.put("sign", appMap.get("sign"));
            map.put("timestamp", appMap.get("timestamp"));
            map.put("partnerid", appMap.get("partnerid"));
            map.put("noncestr", appMap.get("noncestr"));
            map.put("prepayid", appMap.get("prepayid"));
            map.put("appid", appMap.get("appid"));
            map.put("package", "Sign=WXPay");
        } else {
            System.out.println(WXpayCoreRide.getErrMsg(retMap));
        }
        return map;
    }
    /**
     *  微信支付组装app使用参数
     * @param trade_no      订单号
     * @param body          文字内容说明
     * @param total_fee     金额
     * @param wxnotify      回调地址
     * @return
     */
    private static Map<String, String> testUnifiedorder(String trade_no, String body, String total_fee, String wxnotify) {
        Map<String, String> testMap = new HashMap<>();
        testMap.put("body", body);                      // 商品描述
        testMap.put("out_trade_no", trade_no);          // 商户订单号
        testMap.put("total_fee", total_fee);            // 总金额
        testMap.put("spbill_create_ip", "192.168.0.1"); // 终端IP
        testMap.put("notify_url", wxnotify);            // 通知地址
        testMap.put("trade_type", "APP");               // 交易类型
        return WXpayApi.unifiedOrderRetMap(testMap);
    }

}
